﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace C_homework
{
    public partial class Form2 : Form
    {
        private Bank bank;
        private List<IAccount> accounts;

        public Form2()
        {
            InitializeComponent();
            bank = new Bank();

            // 创建一些账户并添加到银行
            var savingsAccount = new Account("S001", 5000);
            var creditAccount = new CreditAccount("C001", 3000, 1000);
            bank.addAccount(savingsAccount);
            bank.addAccount(creditAccount);

            accounts = bank.GetAccounts(); // 获取所有账户
            comboBox1.DataSource = accounts;
            comboBox1.DisplayMember = "AccountNumber";

            // 注册事件
            this.BigMoneyFetched += MainForm_BigMoneyFetched;
        }

        // 事件
        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        protected virtual void OnBigMoneyFetched(IAccount account, decimal amount)
        {
            BigMoneyFetched?.Invoke(this, new BigMoneyArgs(account, amount));
        }

        private void MainForm_BigMoneyFetched(object sender, BigMoneyArgs e)
        {
            MessageBox.Show($"警告：账户 {e.Account.GetAccountNumber()} 取款金额为 {e.Amount}，属于大额取款！");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem is IAccount selectedAccount)
            {
                label1.Text = $"当前余额: {selectedAccount.GetBalance()}";
            }
            if (comboBox1.SelectedItem is CreditAccount creditAccount)
            {
                label2.Text = $"是信用账户，可用额度为: {creditAccount.GetCreditLimit()}";
            }
            else
            {
                label2.Text = "不是信用账户";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (decimal.TryParse(textBox1.Text, out decimal amount))
            {
                if (comboBox1.SelectedItem is IAccount selectedAccount)
                {
                    try
                    {
                        selectedAccount.Withdraw(amount);
                        label1.Text = $"当前余额: {selectedAccount.GetBalance()}";

                        // 触发事件，如果取款金额大于10000
                        if (amount > 10000)
                        {
                            OnBigMoneyFetched(selectedAccount, amount);
                        }
                    }
                    catch (BadCashException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入有效的金额。");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (decimal.TryParse(textBox1.Text, out decimal amount))
            {
                if (comboBox1.SelectedItem is IAccount selectedAccount)
                {
                    selectedAccount.Deposit(amount);
                    label1.Text = $"当前余额: {selectedAccount.GetBalance()}";
                }
            }
            else
            {
                MessageBox.Show("请输入有效的金额。");
            }
        }
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }

    public interface IAccount
    {
        string AccountNumber { get; }
        decimal Balance { get; }
        string GetAccountNumber();
        decimal GetBalance();
        void Deposit(decimal amount);
        void Withdraw(decimal amount);
    }

    public class Account : IAccount
    {
        private string accountNumber;
        protected decimal balance;

        public decimal Balance => balance;
        public string AccountNumber => accountNumber;

        public string GetAccountNumber() => AccountNumber;
        public decimal GetBalance() => balance;

        public void Deposit(decimal amount)
        {
            if (amount <= 0)
            {
                MessageBox.Show("存款金额必须大于0");
            }
            else
            {
                balance += amount;
                MessageBox.Show($"存款成功，当前余额为：{balance}");
            }
        }

        public virtual void Withdraw(decimal amount)
        {
            Random rand = new Random();
            if (rand.NextDouble() < 0.3)
            {
                throw new BadCashException("取款时发生了坏钞！");
            }

            if (amount <= 0)
            {
                MessageBox.Show("取款金额必须大于0");
            }
            else if (amount > Balance)
            {
                MessageBox.Show("余额不足");
            }
            else
            {
                balance -= amount;
                MessageBox.Show($"取款成功，当前余额: {balance}");
            }
        }

        public Account(string accountNumber, decimal balance)
        {
            this.accountNumber = accountNumber;
            this.balance = balance;
        }
    }

    public class CreditAccount : Account
    {
        private decimal creditLimit;

        public decimal GetCreditLimit() => creditLimit;

        public CreditAccount(string accountNumber, decimal balance, decimal creditLimit)
            : base(accountNumber, balance)
        {
            this.creditLimit = creditLimit;
        }

        public override void Withdraw(decimal amount)
        {
            Random rand = new Random();
            if (rand.NextDouble() < 0.3)
            {
                throw new BadCashException("取款时发生了坏钞！");
            }

            if (amount <= 0)
            {
                MessageBox.Show("取款金额必须大于0");
            }
            else if (amount > Balance + creditLimit)
            {
                MessageBox.Show("余额不足");
            }
            else
            {
                balance -= amount;
                MessageBox.Show($"取款成功，当前余额: {balance}");
            }
        }
    }

    public class Bank
    {
        private List<IAccount> accounts;

        public Bank()
        {
            accounts = new List<IAccount>();
        }

        public List<IAccount> GetAccounts() => accounts;

        public void addAccount(IAccount account)
        {
            if (account != null)
            {
                accounts.Add(account);
            }
        }

        public void removeAccount(IAccount account)
        {
            if (accounts.Contains(account))
            {
                accounts.Remove(account);
            }
        }
    }

    public class BigMoneyArgs : EventArgs
    {
        public IAccount Account { get; }
        public decimal Amount { get; }

        public BigMoneyArgs(IAccount account, decimal amount)
        {
            Account = account;
            Amount = amount;
        }
    }

    public class BadCashException : Exception
    {
        public BadCashException(string message) : base(message) { }
    }
}
